package com.itheima.controller;


import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 条件分页查询
     *
     * @param student
     * @return
     */
    @GetMapping
    public Result list(StudentQueryParam student) {
        log.info("查询所有学生信息：{}", student);
        PageResult pageResult = this.studentService.queryPage(student);
        return Result.success(pageResult);
    }

    /**
     * 新增学生信息
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("新增学生信息：{}", student);
        this.studentService.insertStudent(student);
        return Result.success();
    }

    /**
     * 根据id查询学生信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学生信息：{}", id);
        Student student = this.studentService.getStudentById(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息：{}", student);
        this.studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * 根据id删除学生信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("根据id删除学生信息：{}", ids);
        this.studentService.deleteStudent(ids);
        return Result.success();
    }

    /**
     * 根据id扣分
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("根据id{}扣分, 扣分：{}", id, score);
        this.studentService.violation(id, score);
        return Result.success();
    }
}
